package com.li.utilstest

import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.li.utilstest.net.Resource
import com.zy.multistatepage.MultiStateContainer
import com.zy.multistatepage.bindMultiState
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @author : liyun
 * @ClassName: MyViewModel
 * @Package com.li.utilstest
 * @Description:
 * @company: 深圳君南信息系统有限公司
 * @date 2021/7/17 16:51
 * @version v1.0
 */
class MyViewModel: ViewModel() {

    fun testStatePage(code:Int):LiveData<Resource<String>>{
        val date=MutableLiveData<Resource<String>>()
        viewModelScope.launch {
            runCatching {
                date.value=Resource.load()
                delay(3000)
                covert(date,code)
            }.onFailure {
                date.value=Resource.error("网络异常出现异常")
            }
        }
        return date
    }

    fun covert(date:MutableLiveData<Resource<String>>,code:Int){
        when(code){
            0->{
                date.value= Resource.success("请求数据")
            }
            1->date.value=Resource.empty("请求成功！但是返回数据为空")
            2->date.value=Resource.error("服务器出现异常")
            else -> throw Exception("其它问题")
        }
    }
}